const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    front_img:'',
    back_img:'',
    license_img:'',
    name:'',
    phone:'',
    agreement:false,
    maskLayer:true,
    pact:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.accountInfo.audit_status == 2) {
      this.failureDetails();
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  //注册失败详情
  failureDetails: function () {
    var that = this;
    api.POST({
      url: app.globalData.base_url + "user/regDetails",
      success: function (res) {
        if (res.data.code == '') {
          that.setData({
            front_img: res.data.data.id_front_img,
            back_img: res.data.data.id_back_img,
            license_img: res.data.data.license_img,
            name: res.data.data.name,
            phone: res.data.data.mobile,
          })
        } else {
          util.showError(res.data.message);
        }
      }
    })
  },
//协议弹窗
  controller:function(){
    this.setData({
      maskLayer:false
    })
  },
  //不同意协议
  cancel:function(){
    this.setData({
      maskLayer: true,
      pact: false
    })
  },
    //同意协议
  certain: function () {
    this.setData({
      maskLayer: true,
      pact: true
    })
  },

  //上传图片
  editGoodsImgEvent: function (e) {
    var that = this;
    var imgtype = e.currentTarget.dataset.type
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed '], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        util.showLoading("加载中...");
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: app.globalData.base_url + 'img/saveImage',
            filePath: tempFilePaths[i],
            name: 'file',
            success: function (res) {
              util.hiddenLoading();
              var data = JSON.parse(res.data);
              if (imgtype == 'front_img'){
                that.setData({
                  front_img: data.data.url
                })
              } else if (imgtype == 'back_img'){
                that.setData({
                  back_img: data.data.url
                })
              } else if (imgtype == 'license_img'){
                that.setData({
                  license_img: data.data.url
                })
              }
            },
            fail: function () {
              util.hiddenLoading();
            }
          })
        }
      }
    })
  },
  //输入名字
  inputName:function(e){
    var that = this
    var name = e.detail.value;
    this.setData({
      name:name
    })
  },
  //输入手机号
  inputPhone: function (e) {
    var phone = e.detail.value;
    this.setData({
      phone: phone
    })
  },
  //提交注册
  submit:function(){
    var that = this;
    if (this.data.name == '' || this.data.name == undefined) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content:'请输入姓名',
        confirmText: '知道了',
        success: function (res) {
        }
      })
      return false
    }

    if (!(/^1(3|4|5|7|8)\d{9}$/.test(this.data.phone))) {
      // util.showError("请输入正确的手机号");
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请输入正确的手机号',
        confirmText: '知道了',
        success: function (res) {
        }
      })
      return false
    }    
    if (this.data.front_img == '' || this.data.front_img == undefined) {
      // util.showError("请上传身份证正面");
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请上传身份证正面',
        confirmText: '知道了',
        success: function (res) {
        }
      })
      return false
    }

    if (this.data.back_img == '' || this.data.back_img == undefined) {
      // util.showError("请上传身份证反面");
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请上传身份证反面',
        confirmText: '知道了',
        success: function (res) {
        }
      })
      return false
    }

    // if (this.data.license_img == '' || this.data.license_img == undefined) {
    //   // util.showError("请上传营业执照");
    //   wx.showModal({
    //     title: '提示',
    //     showCancel: false,
    //     content: '请上传营业执照',
    //     confirmText: '知道了',
    //     success: function (res) {
    //     }
    //   })
    //   return false
    // }

    if (this.data.pact==false){
      // util.showError("请阅读并同意《酒掌柜服务协议》");
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请阅读并同意《酒掌柜服务协议》',
        confirmText: '知道了',
        success: function (res) {
        }
      })
      return false
    }
    
    var params = {
      name: that.data.name,
      mobile: that.data.phone,
      id_front_img: that.data.front_img,
      id_back_img: that.data.back_img,
      // license_img: that.data.license_img
      address: '四川成都高新',
      shopname :'接口测试邀请者',
      shopmobile : '18628015156'
    };

    api.POST({
      params: params,
      url: app.globalData.base_url + "user/agencyReg",
      success: function (res) {
        if (res.data.code == '') {
          // util.showError("申请成功，等待审核");
          app.globalData.accountInfo.audit_status = 0
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.message,
            confirmText: '知道了',
            success: function (res) {
              wx.navigateBack({
                delta: 2
              })
            }
          })          
        } else {
          // util.showError(res.data.message);
          wx.showModal({
            title: '提示',
            showCancel:false,
            content: res.data.message,
            confirmText: '知道了',
            success: function (res) {
            }
          })    
        }
      }
    })

  }

})